<template>
    <el-row :gutter="20" v-loading="historyLoading"
            element-loading-text="历史信息加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.8)">
        <el-col :span="3">
            <el-alert
                    title="1.表信息"
                    type="info">
            </el-alert>
            <StElDynamicForm ref="tableInfoForm" :helper-form="tableInfoFormHelper" :submit-props="{method: 'get',url: GenUrl.tableInfo,label: '加载表信息'}"
                             @requestSuccess="tableInfoLoadSuccess"
                             :options="tableInfo" :origin-prop="{labelPosition: 'top',inline: false}" :layout="1">
            </StElDynamicForm>
        </el-col>
        <el-col :span="13">
            <el-alert
                    title="2.字段信息"
                    type="info">
            </el-alert>
            <div v-if="tableFields.length == 0" class="st-center st-font-size-sm">暂无数据，点击左侧加载表信息获取</div>
            <template v-else>
                <template v-for="(field,fieldIndex) in tableFields">
                    <StElDynamicForm ref="tableColumnForm" :key="fieldIndex" :submit-props="{useDefault: false}" style="border-bottom: 2px solid gainsboro;"
                                     :options="getTableFieldDynamicFormOptions(field)" :origin-prop="{labelPosition: 'top',inline: false,}" :layout="3">
                    </StElDynamicForm>
                </template>
            </template>
        </el-col>
        <el-col :span="4">
            <el-alert
                    title="3.模板信息"
                    type="info">
            </el-alert>
            <div v-if="templatesLoading" class="st-center st-font-size-sm">加载中...，请等待</div>
            <template v-else-if="templates.length > 0">
                <template v-for="(temp,tempIndex) in templates">
                    <StElDynamicForm ref="templateForm" :key="tempIndex" :submit-props="{useDefault: false}" style="border-bottom: 2px solid gainsboro;"
                                     :options="getTemplateDynamicFormOptions(temp)" :origin-prop="{labelPosition: 'top',inline: false,}" :layout="1">
                    </StElDynamicForm>
                </template>
            </template>
        </el-col>
        <el-col :span="4">
            <el-alert
                    title="4.代码生成"
                    type="info">
            </el-alert>

            <StElDynamicForm ref="generateForm" :submit-props="{method: 'post',url: GenUrl.gen,label: '代码生成',dataFormat:generateDataFormat,msg: '生成成功'}"
                             :options="generateFormOptions" :origin-prop="{labelPosition: 'top',inline: true}" :layout="1">
            </StElDynamicForm>
            <!-- 回到顶部 -->
            <el-backtop></el-backtop>

        </el-col>
    </el-row>

</template>

<script>
    import ConfigDatasourceUrl from '../../../../../web/pc/config/ConfigDatasourceUrl.js'
    import GenUrl from './GenUrl.js'
    import ConfigTemplateUrl from '../../../../pc/config/ConfigTemplateUrl.js'
    import TableGenHistoryUrl from '../../../../pc/history/TableGenHistoryUrl.js'

    export default {
        name: "Gen",
        computed: {
            GenUrl(){
                return GenUrl
            }
        },
        data() {
            return {
                historyLoading: false,
                historyConfig: null,
                tableInfoFormHelper: {
                    tableComment: '',
                    tableCommentRel1: '',
                    tableCommentRel2: '',
                },
                tableInfo:[
                    {
                        field: {
                            name: 'tableName',
                        },
                        element:{
                            label: '名称',
                            required: true,

                        }
                    },
                    {
                        field: {
                            name: 'tableComment',
                        },
                        element:{
                            label: '注释',
                        }
                    },
                    {
                        field: {
                            name: 'tableCommentRel1',
                        },
                        element:{
                            label: 'rel注释1',
                        }
                    },
                    {
                        field: {
                            name: 'tableCommentRel2',
                        },
                        element:{
                            label: 'rel注释2',
                        }
                    },
                    {
                        field: {
                            name: 'configDataSourceId',
                        },
                        element:{
                            label: '数据源配置',
                            type: 'select',
                            required: true,
                            options: {
                                datas: ConfigDatasourceUrl.list
                            }
                        }
                    },
                ],
                tableFields:[],
                templates:[],
                templatesLoading: false,
                generateFormOptions:[
                    {
                        field: {
                            name: 'author'
                        },
                        element: {
                            label: '作者',
                            required: true
                        }
                    },
                    {
                        field: {
                            name: 'tablePrefix'
                        },
                        element: {
                            label: '表前缀',
                        }
                    },
                    {
                        field: {
                            name: 'componentName'
                        },
                        element: {
                            label: '组件名',
                            required: true,
                        }
                    },
                    {
                        field: {
                            name: 'moduleName'
                        },
                        element: {
                            label: '模块名',
                            tips: '一个包/目录分组'
                        }
                    },
                    {
                        field: {
                            name: 'isFileOverride',
                            value: true
                        },
                        element: {
                            label: '覆盖已生成文件',
                            type: 'switch'
                        }
                    },
                    {
                        field: {
                            name: 'isFileDelete',
                            value: false
                        },
                        element: {
                            label: '删除已生成文件',
                            type: 'switch',
                            tips: '该选项会使 覆盖已生成文件 选项失效'
                        }
                    },
                    {
                        field: {
                            name: 'position',
                            value: 'components'
                        },
                        element: {
                            label: '位置',
                            type: 'radioGroup',
                            options: {
                                datas: [
                                    {
                                        id: 'components',
                                        name: 'components',
                                    },
                                    {
                                        id: 'modules',
                                        name: 'modules',
                                    }
                                ]
                            }
                        }
                    },
                    {
                        field: {
                            name: 'tableType',
                            value: 'normal'
                        },
                        element:{
                            label: '类型',
                            required: true,
                            type: 'select',
                            options: {
                                datas: [
                                    {
                                        id: 'rel',
                                        name: 'rel',
                                    },
                                    {
                                        id: 'normal',
                                        name: 'normal',
                                    },
                                    {
                                        id: 'tree',
                                        name: 'tree',
                                    }
                                ]
                            }
                        }
                    },
                ]
            }
        },
        mounted() {

            if(this.$route.query.tableGenHistoryId){
                this.loadTableHistory(this.$route.query.tableGenHistoryId)
            }else {
                this.loadTemplates()
            }
        },

        methods: {
            loadTableHistory(historyId){
                 this.historyLoading = true
                 this.axios.get(this.$stStringTools.replaceb(TableGenHistoryUrl.queryById,{id:  historyId})).then(res=>{
                     let historyConfig = JSON.parse(res.data.configContent)
                     this.historyConfig = historyConfig
                     // 表信息配置
                     let tableInfoFormHelper = {
                         tableName: historyConfig.tableInfo.tableName,
                         tableComment: historyConfig.tableInfo.tableComment,
                         tableCommentRel1: historyConfig.tableInfo.tableCommentRel1,
                         tableCommentRel2: historyConfig.tableInfo.tableCommentRel2,
                         configDataSourceId: historyConfig.tableInfo.configDataSourceId,
                     }
                     this.$refs.tableInfoForm.setFormPropValue(tableInfoFormHelper)
                     // 字段信息
                     this.$refs.tableInfoForm.submitForm()
                     // 模板信息
                     this.loadTemplates()
                     // 生成信息
                     let generateFormHelper = {
                         "author": historyConfig.author,
                         "tablePrefix": historyConfig.tablePrefix,
                         "componentName": historyConfig.componentName,
                         "moduleName": historyConfig.moduleName,
                         "isFileOverride": historyConfig.isFileOverride,
                         "isFileDelete": historyConfig.isFileDelete,
                         "position": historyConfig.position,
                         "tableType": historyConfig.tableType,
                     }
                     this.$refs.generateForm.setFormPropValue(generateFormHelper)
                 })
                .finally(()=>{
                    this.historyLoading = false
                })
            },
            tableInfoLoadSuccess(data) {
                this.tableFields = data.tableColumns
                this.tableInfoFormHelper.tableComment = data.tableComment
                this.tableInfoFormHelper.tableCommentRel1 = data.tableCommentRel1
                this.tableInfoFormHelper.tableCommentRel2 = data.tableCommentRel2

            },
            getFieldHistoryValue(field,propName) {
                let r = field[propName]
                if('orderby' == propName){
                    r = field.isOrderby ? (field.isOrderbyAsc ? 'orderbyAsc':'orderbyDesc') : 'noOrderby'
                }
                if(this.historyConfig && this.historyConfig.tableInfo.tableColumns){
                    this.historyConfig.tableInfo.tableColumns.forEach(c => {
                        if(c.columnName == field.columnName){
                            r = c[propName]
                            if('orderby' == propName){
                                r = c.isOrderby ? (c.isOrderbyAsc ? 'orderbyAsc':'orderbyDesc') : 'noOrderby'
                            }
                        }
                    })
                }
                //
                return r
            },
            getTableFieldDynamicFormOptions(field){
              let r = [
                  {
                      field: {
                          name: 'columnName',
                          value: field.columnName
                      },
                      element: {
                          label: '字段名',
                          type: 'txt'
                      }
                  },
                  {
                      field: {
                          name: 'columnComment',
                          value: this.getFieldHistoryValue(field,'columnComment')
                      },
                      element: {
                          label: '注释'
                      }
                  },
                  {
                      field: {
                          name: 'isRequired',
                          value: this.getFieldHistoryValue(field,'isRequired')
                      },
                      element: {
                          label: '必填',
                          type: 'switch'
                      }
                  },
                  {
                      field: {
                          name: 'isUnique',
                          value: this.getFieldHistoryValue(field,'isUnique')
                      },
                      element: {
                          label: '唯一',
                          type: 'switch'
                      }
                  },
                  {
                      field: {
                          name: 'isForeignKey',
                          value: this.getFieldHistoryValue(field,'isForeignKey')
                      },
                      element: {
                          label: '外键',
                          type: 'switch'
                      }
                  },
                  {
                      field: {
                          name: 'isQueryLike',
                          value: this.getFieldHistoryValue(field,'isQueryLike')
                      },
                      element: {
                          label: '模糊查询',
                          type: 'switch'
                      }
                  },
                  {
                      field: {
                          name: 'orderby',
                          value: this.getFieldHistoryValue(field,'orderby')
                      },
                      element: {
                          label: '排序',
                          type: 'select',
                          options:{
                              datas: [
                                  {
                                      id: 'noOrderby',
                                      name: '不排序'
                                  },
                                  {
                                      id: 'orderbyAsc',
                                      name: '升序'
                                  },
                                  {
                                      id: 'orderbyDesc',
                                      name: '降序'
                                  }
                              ]
                          }
                      }
                  },
                  {
                      field: {
                          name: 'form',
                          value: this.getFieldHistoryValue(field,'form') || ['add','update','query']
                      },
                      element: {
                          label: '表单',
                          type: 'checkBoxGroup',
                          options:{
                              datas: [
                                  {
                                      id: 'add',
                                      name: '添加'
                                  },
                                  {
                                      id: 'update',
                                      name: '更新'
                                  },
                                  {
                                      id: 'query',
                                      name: '查询'
                                  }
                              ]
                          },
                          tips: '暂不支持该选项'
                      }
                  }
              ]

              return r
            },
            loadTemplates(){
                this.templatesLoading = true
                this.axios.get(ConfigTemplateUrl.list).then(res => {
                    this.templates = res.data
                }).finally(()=> {
                    this.templatesLoading = false
                })
            },
            getTemplateHistoryValue(temp,propName) {
                let r = temp[propName]
                if(this.historyConfig && this.historyConfig.tableInfo.templates){
                    this.historyConfig.tableInfo.templates.forEach(c => {
                        if(c.name == temp.name){
                            r = c[propName]
                        }
                    })
                }
                //
                return r
            },
            getTemplateDynamicFormOptions(temp){
                let r = [

                    {
                        field: {
                            name: 'name',
                            value: temp.name
                        },
                        element: {
                            label: '名称',
                            type: 'txt'
                        }
                    },
                    {
                        field: {
                            name: 'templateName',
                            value: temp.templateName
                        },
                        element: {
                            label: '模板名称',
                            type: 'txt'
                        }
                    },
                    {
                        field: {
                            name: 'isGenerate',
                            value: this.getTemplateHistoryValue(temp,'isGenerate') === false ? false : true
                        },
                        element: {
                            label: '生成',
                            type: 'switch'
                        }
                    },
                ]
                return r
            },
            // 代码生成表单
            generateDataFormat(data){
                // 表信息
                data.tableInfo = this.$refs.tableInfoForm.getFormData()
                // 列信息
                if(this.$refs.tableColumnForm){
                    data.tableInfo.tableColumns = []
                    this.$refs.tableColumnForm.forEach(item=>{
                        let columnForm = item.getFormData()
                        // 还有一个值 orderbyDesc

                        columnForm.isOrderby = columnForm.orderby !== 'noOrderby'
                        columnForm.isOrderbyAsc = columnForm.orderby == 'orderbyAsc'

                        data.tableInfo.tableColumns.push(columnForm)
                    })
                }
                // 模板信息
                data.tableInfo.templates = []

                if(this.$refs.templateForm){
                    this.$refs.templateForm.forEach(item=>{
                        data.tableInfo.templates.push(item.getFormData())
                    })
                }
                // templateForm
                return data
            }
        }
    }
</script>

<style scoped>

</style>